import { Injectable } from '@nestjs/common';
import { CreateGroupPlayerScoreDto } from './dto/create-group-player-score.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { GroupPlayerScore } from './entities/group-player-score.entity';
import logger from 'src/common/utils/log';

@Injectable()
export class GroupPlayerScoreService {
  constructor(
    @InjectRepository(GroupPlayerScore)
    private readonly repository: Repository<GroupPlayerScore>,
  ) {}
  async create(createGroupPlayerScoreDto: CreateGroupPlayerScoreDto) {
    const entity = this.repository.create(createGroupPlayerScoreDto);
    await this.repository.save(entity);
  }

  async saveList(groupPlayerScores: Partial<GroupPlayerScore>[]) {
    const entities = groupPlayerScores.map((v) => {
      return this.repository.create(v);
    });
    return await this.repository.save(entities);
  }

  async listByGroupId(groupId: number) {
    return await this.repository.find({
      where: {
        group: {
          id: groupId,
        },
      },
    });
  }

  async listByIds(ids: number[]) {
    return await this.repository
      .createQueryBuilder('groupPlayerScore')
      .where('groupPlayerScore.id IN (:...ids)', { ids })
      .leftJoinAndSelect('groupPlayerScore.player', 'player')
      .getMany();
  }

  async removeByGroupId(groupId: number) {
    await this.repository.delete({
      group: {
        id: groupId,
      },
    });
  }

  async getByGroupIdAndRegistrationIndex(
    groupId: number,
    registrationIndex: string,
  ) {
    return await this.repository.findOne({
      where: {
        group: {
          id: groupId,
        },
        player: {
          registrationIndex,
        },
      },
    });
  }

  findAll() {
    return `This action returns all groupPlayerScore`;
  }

  findOne(id: number) {
    return `This action returns a #${id} groupPlayerScore`;
  }

  remove(id: number) {
    return `This action removes a #${id} groupPlayerScore`;
  }
  async removeIds(ids: number[]) {
    return this.repository.delete(ids);
  }
}
